<template>
  <div class="exchangePAge">
    <div style="color: #101010; font-weight: 600">组合购专属链路</div>
    <div style="margin-bottom: 15px; display: flex; justify-content: flex-end">
      <el-button type="primary" @click="addExchangeGoods()">创建商品</el-button>
    </div>
    <div>
      <el-table :data="addGoodsList" border style="width: 100%">
        <el-table-column
          label="ID"
          type="index"
          :index="indexMethod"
          align="center"
        />
        <el-table-column
          prop="name"
          label="组合购链路名称"
          width="200"
          align="center"
        >
        </el-table-column>
        <el-table-column prop="code" label="code" width="180" align="center">
        </el-table-column>
        <el-table-column
          prop="extensionParams.price"
          label="价格"
          width="160"
          align="center"
        >
        </el-table-column>
        <el-table-column
          prop="editTime"
          label="更新日期"
          width="210"
          align="center"
        >
        </el-table-column>
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-button
              type="primary"
              size="mini"
              @click="editAddGoods(scope.row)"
              >查询</el-button
            >
            <el-button @click="deleteGoods(scope.row)" type="danger" size="mini"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <div class="block" style="margin-top: 30px">
        <el-pagination
          mini
          layout="total, prev, pager, next, jumper"
          :current-page.sync="currentPage"
          :page-size="iDisplayLength"
          :total="totalPage"
          @current-change="handleCurrentChange"
        >
        </el-pagination>
      </div>
    </div>
    <groupAdd
      v-if="addOrEdit == 1"
      :addOrEdit="addOrEdit"
      @getAddGoodsList="getAddGoodsList"
    ></groupAdd>
    <groupEdit
      v-if="addOrEdit == 2"
      :editGoodsCont="editGoodsCont"
      :addOrEdit="addOrEdit"
      @getAddGoodsList="getAddGoodsList"
    ></groupEdit>
  </div>
</template>
<script>
import groupAdd from "./groupAdd.vue";
import groupEdit from "./groupEdit.vue";
import requestAxios from "../../../http";
export default {
  name: "groupPage",
  components: {
    groupAdd,
    groupEdit,
  },
  data() {
    return {
      addOrEdit: false, //添加新的商品还是编辑
      editGoodsCont: {}, //编辑的商品信息
      addGoodsList: [],
      currentPage: 1,
      iDisplayStart: 0,
      iDisplayLength: 10,
      totalPage: 0,
    };
  },
  mounted() {
    this.getAddGoodsList();
  },
  methods: {
    indexMethod(index) {
      return (this.currentPage - 1) * this.iDisplayLength + index + 1;
    },

    //添加
    addExchangeGoods() {
      this.addOrEdit = 1;
    },
    //编辑
    editAddGoods(item) {
      requestAxios
        .fetchGet("/api/v2.1/trackingProjectChannel/pepsi/get?id=" + item.id, {
          headers: {},
        })
        .then((data) => {
          if (data.status == 200 && data.data.success) {
            console.log("编辑获取组合够列表", data.data);
            this.addOrEdit = 2;
            this.editGoodsCont = data.data.data;
            console.log(this.editGoodsCont, "1111111111134445");
          }
        })
        .catch((err) => {
          console.log(err);
        });
    },
    //获取列表信息
    getAddGoodsList() {
      let that = this;
      let obj = null;
      obj = {
        pageQuery: {
          iDisplayStart: (that.currentPage - 1) * that.iDisplayLength,
          iDisplayLength: that.iDisplayLength,
        },
      };
      console.log(obj.pageQuery.iDisplayStart, "多少条");
      requestAxios
        .fetchPost("/api/v2.1/trackingProjectChannel/pepsi/search", obj, {
          headers: {},
        })
        .then((data) => {
          console.log(data, "____)))))))))))))))))))))))))0");
          if (data.status == 200 && data.data.success) {
            console.log("获取列表", data.data);
            that.addGoodsList = data.data.data.aaData;
            that.totalPage = data.data.data.iTotalDisplayRecords;
          }
        })
        .catch((err) => {
          console.log(err);
        });
    },

    // 显示第几页
    handleCurrentChange(val) {
      // 改变默认的页数
      this.currentPage = val;
      console.log(this.currentPage, "第几页");
      this.getAddGoodsList();
    },

    //删除
    deleteGoods(item) {
      console.log("删除某个奖品", item);
      this.$confirm("此操作将永久删除, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.deleteConfirm(item.id);
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    deleteConfirm(id) {
      requestAxios
        .fetchGet(
          "/api/v2.1/trackingProjectChannel/delete/?ids=" + id,
          {},
          {
            headers: {},
          }
        )
        .then((data) => {
          if (data.status == 200 && data.data.success) {
            console.log("删除奖品", data.data);
            this.$message({
              type: "success",
              message: "删除成功!",
            });
            let totalPage = Math.ceil(
              (this.totalPage - 1) / this.iDisplayLength
            );
            let currengpage =
              this.iDisplayStart > totalPage ? totalPage : this.iDisplayStart;
            this.iDisplayStart = currengpage < 1 ? 1 : currengpage;
            this.getAddGoodsList();
          } else {
            console.log("删除奖品", data.data);
            this.tip(false, data.data.errorMsg || data.data.data);
          }
        })
        .catch((err) => {
          console.log(err);
        });
    },
    //提示的通用方法
    tip(state, text) {
      if (state) {
        this.$message({
          showClose: true,
          message: text,
          type: "success",
        });
      } else {
        this.$message({
          showClose: true,
          message: text,
          type: "error",
        });
      }
    },
  },
};
</script>
<style scoped>
</style>
